<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

<script>
    var baseDate = new Date();
    var dateObj = {
        currentDay: [],
        thrityDay:[],
        sevenDay:[],
        dateArray: [],
        weekArray: [],
        monthArray: []
    }
    Date.prototype.format = function(fmt) {
        var o = {
            "M+" : this.getMonth()+1,                 //月份
            "d+" : this.getDate(),                    //日
            "h+" : this.getHours(),                   //小时
            "m+" : this.getMinutes(),                 //分
            "s+" : this.getSeconds(),                 //秒
            "q+" : Math.floor((this.getMonth()+3)/3), //季度
            "S"  : this.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt)) {
            fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
        }
        for(var k in o) {
            if(new RegExp("("+ k +")").test(fmt)){
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
            }
        }
        return fmt;
    }
    Date.prototype.recoverCurrentDate = function(baseDate) {
        if (typeof (baseDate) !== "undefined") {
            console.log("baseDate " + baseDate.getDate());
            this.setDate(baseDate.getDate());
            this.setMonth(baseDate.getMonth())
            this.setYear(baseDate.getFullYear())
        }
    }
    Date.prototype.format = function(fmt,mdiff,ddiff,baseDate) {

        if (typeof (baseDate) !== "undefined") {
            console.log("baseDate " + baseDate.getDate());
            this.setDate(baseDate.getDate());
            this.setMonth(baseDate.getMonth())
            this.setYear(baseDate.getFullYear())
                    }
        this.setDate( this.getDate()-1-ddiff);
        var o = {
            "M+" : this.getMonth()+1-mdiff,                 //月份
            "d+" : this.getDate(),                    //日
            "h+" : this.getHours(),                   //小时
            "m+" : this.getMinutes(),                 //分
            "s+" : this.getSeconds(),                 //秒
            "q+" : Math.floor((this.getMonth()+3)/3), //季度
            "S"  : this.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt)) {
            fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
        }
        for(var k in o) {
            if(new RegExp("("+ k +")").test(fmt)){
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
            }
        }
        return fmt;
    }

    function assembleDateArray() {
        var now = new Date();
        var startDate = new Date();
        var diffDays = DateDiff(now.format("yyyy-MM-dd", 0, 0), startDate.format("yyyy-MM-dd", 3, 0));
        console.log("diffDays " + diffDays);
        dateObj.currentDay[0] = now.format("yyyy-MM-dd", 0, 0,baseDate) + "|"+now.format("yyyy-MM-dd",0, 0,baseDate);
        now.recoverCurrentDate(baseDate)
        dateObj.sevenDay[0] = now.format("yyyy-MM-dd", 0, 0,baseDate) + "|" + now.format("yyyy-MM-dd", 0, 6,baseDate);
        now.recoverCurrentDate(baseDate)
        dateObj.thrityDay[0] = now.format("yyyy-MM-dd", 0, 0,baseDate) + "|" + now.format("yyyy-MM-dd", 0, 29,baseDate);
        now.recoverCurrentDate(baseDate)
        for(k=1;k<=12;k++){
            if((now.getMonth()+1)>k){
                assembleMonthRange(now.getFullYear(),(now.getMonth()+1-k))
            }else{
                assembleMonthRange(now.getFullYear()-1,k)
            }
        }
        assembleMonthRange(now.getFullYear()-1,now.getMonth(),true)
        for (i = 0; i <= diffDays; i++) {
            //console.log("in loop " + now.format("yyyy-MM-dd", 0, i, baseDate))
            dateObj.dateArray[i] = now.format("yyyy-MM-dd", 0, i, baseDate)+"|"+ now.format("yyyy-MM-dd", 0, i, baseDate);
        }
        now.recoverCurrentDate(baseDate)
        var weekTemp = Math.floor((diffDays - now.getDay())/7);
        for(j=0;j<weekTemp;j++){
            dateObj.weekArray[j] = now.format("yyyy-MM-dd", 0, (j+1)*7-1, baseDate)+"|"+now.format("yyyy-MM-dd", 0, now.getDay()-1+ j*7, baseDate);

        }
        now.recoverCurrentDate(baseDate)
    }

    function DateDiff(d1,d2) {
        var day = 24 * 60 * 60 * 1000;
        try {
            var dateArr = d1.split("-");
            var checkDate = new Date();
            checkDate.setFullYear(dateArr[0], dateArr[1] - 1, dateArr[2]);
            var checkTime = checkDate.getTime();

            var dateArr2 = d2.split("-");
            var checkDate2 = new Date();
            checkDate2.setFullYear(dateArr2[0], dateArr2[1] - 1, dateArr2[2]);
            var checkTime2 = checkDate2.getTime();

            var cha = (checkTime - checkTime2) / day;
            return cha;
        } catch (e) {
            return false;
        }
    }

    function assembleMonthRange(year,month,isLastYearMonth) {
        var nowdays = new Date();
        if (month < 10) {
            month = "0" + month;
        }
        var firstDay = year + "-" + month + "-" + "01";//
        var myDate = new Date(year, month, 0);
        var lastDay = year + "-" + month + "-" + myDate.getDate();//
        if(typeof (isLastYearMonth)!== "undefined"){
            dateObj.monthArray[12]= firstDay+"|"+lastDay
        }else{
            dateObj.monthArray[month-1]= firstDay+"|"+lastDay
        }

    }
    assembleDateArray();

</script>

</body>
</html>